"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ProFormListContainer = void 0;

var _regeneratorRuntime2 = _interopRequireDefault(require("@babel/runtime/helpers/regeneratorRuntime"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _react = require("react");

var _jsxRuntime = require("react/jsx-runtime");

var _icons = require("@ant-design/icons");

var _proProvider = require("@ant-design/pro-provider");

var _proUtils = require("@ant-design/pro-utils");

var _antdV = require("antd-v4");

var _omit = _interopRequireDefault(require("omit.js"));

var _ListItem = require("./ListItem");

var ProFormListContainer = function ProFormListContainer(props) {
  var intl = (0, _proProvider.useIntl)();
  var creatorButtonProps = props.creatorButtonProps,
      prefixCls = props.prefixCls,
      children = props.children,
      creatorRecord = props.creatorRecord,
      action = props.action,
      fields = props.fields,
      actionGuard = props.actionGuard,
      max = props.max,
      fieldExtraRender = props.fieldExtraRender,
      meta = props.meta;
  var fieldKeyMap = (0, _react.useRef)(new Map());

  var _useState = (0, _react.useState)(false),
      _useState2 = (0, _slicedToArray2.default)(_useState, 2),
      loading = _useState2[0],
      setLoading = _useState2[1];

  var uuidFields = (0, _react.useMemo)(function () {
    return fields.map(function (field) {
      var _fieldKeyMap$current, _fieldKeyMap$current3;

      if (!((_fieldKeyMap$current = fieldKeyMap.current) === null || _fieldKeyMap$current === void 0 ? void 0 : _fieldKeyMap$current.has(field.key.toString()))) {
        var _fieldKeyMap$current2;

        (_fieldKeyMap$current2 = fieldKeyMap.current) === null || _fieldKeyMap$current2 === void 0 ? void 0 : _fieldKeyMap$current2.set(field.key.toString(), (0, _proUtils.nanoid)());
      }

      var uuid = (_fieldKeyMap$current3 = fieldKeyMap.current) === null || _fieldKeyMap$current3 === void 0 ? void 0 : _fieldKeyMap$current3.get(field.key.toString());
      return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, field), {}, {
        uuid: uuid
      });
    });
  }, [fields]);
  /**
   * 根据行为守卫包装action函数
   */

  var wrapperAction = (0, _react.useMemo)(function () {
    var wrapAction = (0, _objectSpread2.default)({}, action);
    var count = uuidFields.length;

    if (actionGuard === null || actionGuard === void 0 ? void 0 : actionGuard.beforeAddRow) {
      wrapAction.add = /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/(0, _regeneratorRuntime2.default)().mark(function _callee() {
        var _len,
            rest,
            _key,
            success,
            _args = arguments;

        return (0, _regeneratorRuntime2.default)().wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                for (_len = _args.length, rest = new Array(_len), _key = 0; _key < _len; _key++) {
                  rest[_key] = _args[_key];
                }

                _context.next = 3;
                return actionGuard.beforeAddRow.apply(actionGuard, rest.concat([count]));

              case 3:
                success = _context.sent;

                if (!success) {
                  _context.next = 6;
                  break;
                }

                return _context.abrupt("return", action.add.apply(action, rest));

              case 6:
                return _context.abrupt("return", false);

              case 7:
              case "end":
                return _context.stop();
            }
          }
        }, _callee);
      }));
    }

    if (actionGuard === null || actionGuard === void 0 ? void 0 : actionGuard.beforeRemoveRow) {
      wrapAction.remove = /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/(0, _regeneratorRuntime2.default)().mark(function _callee2() {
        var _len2,
            rest,
            _key2,
            success,
            _args2 = arguments;

        return (0, _regeneratorRuntime2.default)().wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                for (_len2 = _args2.length, rest = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
                  rest[_key2] = _args2[_key2];
                }

                _context2.next = 3;
                return actionGuard.beforeRemoveRow.apply(actionGuard, rest.concat([count]));

              case 3:
                success = _context2.sent;

                if (!success) {
                  _context2.next = 6;
                  break;
                }

                return _context2.abrupt("return", action.remove.apply(action, rest));

              case 6:
                return _context2.abrupt("return", false);

              case 7:
              case "end":
                return _context2.stop();
            }
          }
        }, _callee2);
      }));
    }

    return wrapAction;
  }, [action, actionGuard, uuidFields]);
  var creatorButton = (0, _react.useMemo)(function () {
    if (creatorButtonProps === false || uuidFields.length === max) return null;

    var _ref3 = creatorButtonProps || {},
        _ref3$position = _ref3.position,
        position = _ref3$position === void 0 ? 'bottom' : _ref3$position,
        _ref3$creatorButtonTe = _ref3.creatorButtonText,
        creatorButtonText = _ref3$creatorButtonTe === void 0 ? intl.getMessage('editableTable.action.add', '添加一行数据') : _ref3$creatorButtonTe;

    return (0, _jsxRuntime.jsx)(_antdV.Button, (0, _objectSpread2.default)((0, _objectSpread2.default)({
      className: "".concat(prefixCls, "-creator-button-").concat(position),
      type: "dashed",
      loading: loading,
      block: true,
      icon: (0, _jsxRuntime.jsx)(_icons.PlusOutlined, {})
    }, (0, _omit.default)(creatorButtonProps || {}, ['position', 'creatorButtonText'])), {}, {
      onClick: function () {
        var _onClick = (0, _asyncToGenerator2.default)( /*#__PURE__*/(0, _regeneratorRuntime2.default)().mark(function _callee3() {
          var index;
          return (0, _regeneratorRuntime2.default)().wrap(function _callee3$(_context3) {
            while (1) {
              switch (_context3.prev = _context3.next) {
                case 0:
                  setLoading(true); // 如果不是从顶部开始添加，则插入的索引为当前行数

                  index = uuidFields.length; // 如果是顶部，加到第一个，如果不是，为空就是最后一个

                  if (position === 'top') index = 0;
                  _context3.next = 5;
                  return wrapperAction.add((0, _proUtils.runFunction)(creatorRecord) || {}, index);

                case 5:
                  setLoading(false);

                case 6:
                case "end":
                  return _context3.stop();
              }
            }
          }, _callee3);
        }));

        function onClick() {
          return _onClick.apply(this, arguments);
        }

        return onClick;
      }(),
      children: creatorButtonText
    }));
  }, [creatorButtonProps, uuidFields.length, max, intl, prefixCls, loading, wrapperAction, creatorRecord]);
  return (0, _jsxRuntime.jsxs)("div", {
    style: {
      width: 'max-content',
      maxWidth: '100%',
      minWidth: '100%'
    },
    children: [creatorButtonProps !== false && (creatorButtonProps === null || creatorButtonProps === void 0 ? void 0 : creatorButtonProps.position) === 'top' && creatorButton, uuidFields.map(function (field, index) {
      return /*#__PURE__*/(0, _react.createElement)(_ListItem.ProFormListItem, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, props), {}, {
        key: field.uuid,
        field: field,
        index: index,
        action: wrapperAction,
        count: uuidFields.length
      }), children);
    }), fieldExtraRender && fieldExtraRender(wrapperAction, meta), creatorButtonProps !== false && (creatorButtonProps === null || creatorButtonProps === void 0 ? void 0 : creatorButtonProps.position) !== 'top' && creatorButton]
  });
};

exports.ProFormListContainer = ProFormListContainer;